<template>
    <a-modal :title="title" :width="450" :destroyOnClose="true" :maskClosable="false"
             :visible="visible" @cancel="handleCancel"
    >
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
            <!-- 登录账号 -->
            <a-form-model-item label="规则组代码" prop="code" ref="code">
                <a-input placeholder="请设置计费规则组代码"
                         :maxLength="20"
                         v-model="form.code" :disabled="isUpdate"/>
            </a-form-model-item>
            <a-form-model-item label="规则组名称" prop="name" ref="name">
                <a-input
                        :maxLength="20"
                        placeholder="请设置计费规则组名称" v-model="form.name"/>
            </a-form-model-item>


        </a-form-model>
        <!-- Modal 页脚 -->
        <!-- Modal 页脚 -->
        <template slot="footer">
            <a-button @click="handleCancel">关闭</a-button>
            <a-button type="primary" :loading="loading" @click="onSubmit">保存</a-button>
        </template>


    </a-modal>
</template>
<script>
    import {FeeGroupAdd, FeeGroupUpdate} from '../../public/http.js';
    export default {
        props: ['editRecord'],
        data() {
            return {
                isUpdate: false, //是否是更新模式
                loading: false, //按钮Loading
                title: '新增计费规则组',
                visible: true, //modal 显示状态
                labelCol: {
                    xs: {
                        span: 24
                    },
                    sm: {
                        span: 5
                    }
                },
                wrapperCol: {
                    xs: {
                        span: 24
                    },
                    sm: {
                        span: 16
                    }
                },
                form: {
                    code: null,
                    name: null,

                },
                rules: {
                    code: [
                        {
                            required: true,
                            message: '请设置计费规则组代码',
                            trigger: 'blur'
                        },
                        {
                            max: 20,
                            message: '计费规则组代码请设置在20个字符以内',
                            trigger: 'blur'
                        },
                        {
                            pattern: /^[a-zA-Z0-9]+$/,
                            message: '规则代码只允许填写数字或者字母',
                            trigger: 'blur'
                        }
                    ],
                    name: [
                        {
                            required: true,
                            message: '请设置计费规则组名称',
                            trigger: 'blur'
                        },
                        {
                            max: 20,
                            message: '计费规则组名称请设置为20个字符以内',
                            trigger: 'blur'
                        }
                    ]
                },

            };
        },
        methods: {


            //表单提交事件
            onSubmit() {
                this.$refs.ruleForm.validate(valid => {
                    //验证成功后进行数据提交
                    if (valid) {
                        this.loading = true;
                        if (this.isUpdate) {
                           FeeGroupUpdate(this.form.id,this.form).then(res=>{
                               this.$exMod.ajaxMessage(res);
                               if (res.code === 0) {
                                   this.handleCancel(true,true);
                               }
                           })
                        }
                        else {
                            FeeGroupAdd(this.form).then(res=>{
                                this.$exMod.ajaxMessage(res);
                                if (res.code === 0) {
                                    this.handleCancel(true,true);
                                }
                            })
                        }
                        this.loading = false;

                    }
                });
            },
            //关闭弹窗
            handleCancel(render = false,refresh=false) {
                this.visible = false;
                let obj={render,refresh,activeRecor:true};
                if(refresh){
                    obj.activeRecor=this.form
                }

                this.$emit('closeFun', obj);
            },
            //表单初始化
            fromVal() {
                let obj = {...this.editRecord};
                if (obj.id) {
                    this.form = obj;
                    this.isUpdate = true;
                    this.title = '修改规则分组';
                }
            },
            //数据初始化
            async init() {

                this.fromVal();
            }
        },
        mounted() {
            this.init();
        }
    };
</script>
